import vueCom from '../main.js'
import axios from 'axios';
import glo_loading from '../loading/index' //loading组件的简单封装
import {apiLoading} from '../config/index' //全局的页面跳转loading是否启用

const service = axios.create({
    baseURL:WINDOW_BASE_URL,
    timeout: 100000
})

service.interceptors.request.use( config => {
    let token = localStorage.getItem('Authorization')
    if (!config.headers.hasOwnProperty('Authorization') && token) {
        config.headers.Authorization = token;
    }
    apiLoading.isShow ? glo_loading.loadingShow() : ''//全局loading是否启用
    return config;
}, error => {
    console.log(error);
    apiLoading.isShow ? glo_loading.loadingHide() : ''
    return Promise.reject();
})

service.interceptors.response.use(response => {
    if(response.status === 200){
        apiLoading.isShow ? glo_loading.loadingHide() : ''
        return response.data;
    }else{
        Promise.reject();
    }
}, error => {
    console.log(error);

    apiLoading.isShow ? glo_loading.loadingHide() : ''

    return Promise.reject();
})

export default service;
